package de.mhus.lib.mandala;

import java.util.LinkedList;

import de.mhus.lib.core.MString;
import de.mhus.lib.mandala.attributes.ArrayAttribute;
import de.mhus.lib.mandala.attributes.BooleanAttribute;
import de.mhus.lib.mandala.attributes.MultiAttribute;
import de.mhus.lib.mandala.functions.FunctionCall;
import de.mhus.lib.mandala.tag.TagBindData;
import de.mhus.lib.mandala.tag.BindCss;
import de.mhus.lib.mandala.tag.BindValue;
import de.mhus.lib.mandala.tag.BindVisible;
import de.mhus.lib.mandala.tag.BindWith;
import de.mhus.lib.mandala.tag.ClickAction;
import de.mhus.lib.mandala.tag.Data;
import de.mhus.lib.mandala.tag.Options;

public class TagAttributes {

	public enum EMPHASIS {DEFAULT,DANGER,WARNING,INFO,SUCCESS};
	public enum SIZE {LG,DEFAULT,SM,XS}
	
	private String styleClass;
	private String extraStyle;
	private EMPHASIS emphasis;
	private String styleBase;
	private SIZE sizeStyle;
	private LinkedList<TagBindData> bindList = new LinkedList<TagBindData>();
	
	public String getStyleClass() {
		return styleClass;
	}
	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}
	public String getExtraStyle() {
		return extraStyle;
	}
	public void setExtraStyle(String extraStyle) {
		this.extraStyle = extraStyle;
	}
	public void setClickAction(FunctionCall clickAction) {
		bindList.add(new ClickAction(clickAction));
	}
	public void setBindValue(Attribute bindValue) {
		bindList.add(new BindValue(bindValue));
	}
	public void setBindVisible(BooleanAttribute bindVisible) {
		bindList.add(new BindVisible(bindVisible));
	}
	public void setBindCss(MultiAttribute bindCss) {
		bindList.add(new BindCss(bindCss));
	}
	public void setBindWith(Attribute bindWith) {
		bindList.add(new BindWith(bindWith));
	}
	public EMPHASIS getEmphasis() {
		return emphasis;
	}
	public void setEmphasis(EMPHASIS emphasis) {
		this.emphasis = emphasis;
	}
	public String getStyleBase() {
		return styleBase;
	}
	public void setStyleBase(String styleBase) {
		this.styleBase = styleBase;
	}
	public SIZE getSizeStyle() {
		return sizeStyle;
	}
	public void setSizeStyle(SIZE sizeStyle) {
		this.sizeStyle = sizeStyle;
	}
	
	public String findStyleClass(String c) {
		if (MString.isEmpty(c))
			return getStyleClass();
		if (MString.isEmpty(getStyleClass()))
			return c;
		return getStyleClass() + " " + c;
	}
	
	public boolean hasDataBind() {
		return bindList.size() > 0;
	}
	public void setOptions(ArrayAttribute options) {
		bindList.add(new Options(options));
	}
	
	public void setData(ArrayAttribute data) {
		bindList.add(new Data(data));
	}

	public void addBind(TagBindData bind) {
		bindList.add(bind);
	}
	
	public TagBindData getBind(Class<? extends TagBindData> clazz) {
		for (TagBindData bind : bindList)
			if (bind.getClass().equals(clazz)) return bind;
		return null;
	}
	
	public void renderDataBind(Response out, String dataBind, String name, String type) {
		if (hasDataBind() || MString.isSet(dataBind)) {
			out.print(" data-bind=\"");
			boolean needComma = false;
			if (MString.isSet(dataBind)) {
				if (needComma) out.print(",");
				out.print(dataBind);
				needComma = true;
			}
			for (TagBindData bind : bindList) {
				if (needComma) out.print(",");
				bind.doRender(out, name, type);
				needComma = true;
			}
			out.print("\"");
		}
		
	}
	public MultiAttribute createBindCss() {
		BindCss bind = (BindCss) getBind(BindCss.class);
		if (bind == null) {
			bind = new BindCss(new MultiAttribute());
			addBind(bind);
		}
		return bind.getBindCss();
	}


}
